//
//  VistaEditarPicoYPlaca.m
//  Pico y placa
//
//  Created by Mario Franco on 12/13/12.
//  Copyright (c) 2012 Mario Franco. All rights reserved.
//

#import "VistaEditarPicoYPlaca.h"
#import "SessionManager.h"
#import "TipoVehiculo.h"
#import "VistaPicoYPlaca.h"
#import "VistaCiudadesPicoYPlaca.h"

@interface VistaEditarPicoYPlaca ()
{
    SessionManager *principal;
}

@end

@implementation VistaEditarPicoYPlaca
@synthesize primeraVez;
@synthesize btnOK;
@synthesize tabla;
@synthesize bannerView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{   
    [super viewDidLoad];

    
    principal = [SessionManager darInstancia];
    [self moveBannerViewOffscreen];
    
    [tabla setEditing:YES animated:YES];
    if([principal.darFavoritos count]==0)
        btnOK.enabled = NO;
}

-(void)viewDidAppear:(BOOL)animated
{
    if([principal.darFavoritos count]==0 && primeraVez)
        [self performSegueWithIdentifier:@"verCiudadesPicoYPlaca" sender:self];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [principal.darFavoritos count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
    }
    
    TipoVehiculo *v = [[principal darFavoritos] objectAtIndex:[indexPath row]];
    Ciudad *c = v.ciudad;
    NSMutableString *cadena = [NSMutableString stringWithString:c.nombre];
    [cadena appendString:@" — "];
    [cadena appendString:v.tipo];
    cell.textLabel.text = cadena;
    cell.textLabel.font=[UIFont fontWithName:@"ArialMT" size:17];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [principal establecerFavorito:[principal.darFavoritos objectAtIndex:indexPath.row]];
    [self performSegueWithIdentifier:@"verPicoYPlaca" sender:self];
}

 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
 {
     TipoVehiculo *v = [[principal darFavoritos] objectAtIndex:fromIndexPath.row];
     [[principal darFavoritos] removeObjectAtIndex:fromIndexPath.row];
     [[principal darFavoritos] insertObject:v atIndex:toIndexPath.row];
 }

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
 {
     return YES;
 }


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle==UITableViewCellEditingStyleDelete)
    {
        TipoVehiculo *v = [[principal darFavoritos] objectAtIndex:indexPath.row];
        if([v isEqual:[principal darFavorito]])
           [principal establecerFavorito:[[TipoVehiculo alloc]init]];        
        
        [[principal darFavoritos] removeObject:v];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    [tableView reloadData];

    if([principal.darFavoritos count]==0)
        btnOK.enabled = NO;
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)moveBannerViewOffscreen
{
    //Hacer que la subvista1 ocupe el espacio dejado por el bannerView
    CGRect frameOriginalTabla = tabla.frame;
    CGFloat nuevoHeight = self.view.frame.size.height-44;
    CGRect frameNuevoTabla = frameOriginalTabla;
    frameNuevoTabla.size.height = nuevoHeight;
    
    //Posicionar el bannerView debajo de la subvista1 (offscreen)
    CGRect frameNuevoBanner = bannerView.frame;
    frameNuevoBanner.origin.y = nuevoHeight+44;
    
    tabla.frame = frameNuevoTabla;
    bannerView.frame = frameNuevoBanner;
}

-(void)moveBannerViewOnscreen
{
    CGRect frameNuevoBanner = bannerView.frame;
    frameNuevoBanner.origin.y = self.view.frame.size.height - frameNuevoBanner.size.height;
    
    CGRect frameOriginalTabla = tabla.frame;
    CGFloat nuevoHeight = tabla.frame.size.height - frameNuevoBanner.size.height;
    CGRect frameNuevoTabla = frameOriginalTabla;
    frameNuevoTabla.size.height = nuevoHeight;
    
    [UIView beginAnimations:@"BannerViewIntro" context:NULL];
    tabla.frame = frameNuevoTabla;
    bannerView.frame = frameNuevoBanner;
    [UIView commitAnimations];
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    [self moveBannerViewOffscreen];
}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    [self moveBannerViewOnscreen];
}

- (void) viewWillDisappear:(BOOL)animated
{
    [bannerView removeFromSuperview];
    bannerView.delegate = nil;
    bannerView = nil;
}

@end
